﻿
//Bom, operates the browser object.
LightScript.using("LightScript.Core", function()
{
    var thisClass = LightScript.Core.createClass("LightScript.Bom");

    //# Private Members
    var _userAgent = navigator.userAgent.toLowerCase();

    //# Constructions

    //# Public Members
    thisClass.browser = 
    {
        version: (_userAgent.match(/.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/) || [0, "0"])[1],
        safari: /webkit/.test(_userAgent),
        opera: /opera/.test(_userAgent),
        msie: /msie/.test(_userAgent) && !/opera/.test(_userAgent),
        firefox: /firefox/.test(_userAgent),
        mozilla: /mozilla/.test(_userAgent) && !/(compatible|webkit)/.test(_userAgent)
    };

    //# Private Methods

    //# Public Methods
    thisClass.readCookie = readCookie;
    function readCookie(name, item)
    {
        var cook = document.cookie;
        var reg;
        if (item)
            reg = new RegExp("(^|; )" + escape(name) + "=" + "[^; ]*" + escape(item) + "=([^&; ]*)(&|; |$)");
        else
            reg = new RegExp("(^|; )" + escape(name) + "=" + "([^; ]*)" + "([^; ]*)(; |$)");
        var res = cook.match(reg);

        return res ? unescape(res[2]) : null;
    }

    thisClass.writeCookie = writeCookie;
    function writeCookie(name, item, value, expires, path, domain, secure)
    {
        name = escape(name);
        item = escape(item);
        value = escape(value);
        if (!path)
            path = "/";

        var cook = document.cookie;
        var reg = new RegExp("(^|; )" + name + "=([^; ]*)(|; |$)");
        var cookExist = cook.match(reg);
        var cookNew;

        if (cookExist)
        {
            cookExist = cookExist[2];
            reg = new RegExp("(^|&)" + item + "=([^&]*)(&|$)");
            if (cookExist.search(reg) >= 0)
                cookNew = cookExist.replace(cookExist.match(reg)[0].replace(/&/g, ""), item + "=" + value);
            else
                cookNew = cookExist + "&" + item + "=" + value;
        }
        else
            cookNew = item + "=" + value;

        document.cookie = (name + "=" + cookNew) +
                ((expires) ? ";expires=" + expires.toGMTString() : "") +
                ((path) ? ";path=" + path : "") +
                ((domain) ? ";domain=" + domain : "") +
                ((secure) ? ";secure" : "");
    }

    //# Shortcuts
});
